package a07_二叉树;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * <p>
 * a03_层序遍历01_二叉树的层序遍历
 * </p>
 *
 * @author flyduck
 * @since 2024-08-20
 */
public class a05_层序遍历01_二叉树的层序遍历复习1 {

    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("7");
        System.out.println(bigDecimal.compareTo(BigDecimal.ZERO));
        TreeNode root = new TreeNode(
                6,
                new TreeNode(4,new TreeNode(1),new TreeNode(3)),
                new TreeNode(7,new TreeNode(5),new TreeNode(8)));
//        TreeNode root = new TreeNode(
//                6,
//                new TreeNode(4),
//                new TreeNode(7,new TreeNode(5),new TreeNode(8)));
        List<List<Integer>> lists = levelOrder(root);
        System.out.println(lists);
    }


    //      6
    //  4       7
    //1   3   5   8
    //queue里面有n个元素

    //吐出一个，将它的左右孩子加入队列
    //size--
    public static List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> resultList = new ArrayList<>();
        if (root == null) {
            return resultList;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int popSize = queue.size();
            List<Integer> result = new ArrayList<>();
            while (popSize > 0) {
                TreeNode pollNode = queue.poll();
                result.add(pollNode.val);
                if (pollNode.left != null) {
                    queue.add(pollNode.left);
                }
                if (pollNode.right != null) {
                    queue.add(pollNode.right);
                }
                popSize--;
            }
            resultList.add(result);
        }
        return resultList;

    }
}
